package com.ylbh.modules.member.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.member.vo.ConsumerVO;
import com.ylbh.common.pojo.member.dto.select.ConsumerSelectDTO;
import com.ylbh.common.pojo.member.vo.InvitationRecordExcel;
import com.ylbh.common.pojo.member.vo.InviteMembersVO;
import com.ylbh.common.utils.ExcelUtils;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.pojo.member.AccExcel;
import com.ylbh.modules.member.mapper.ConsumerMapper;
import com.ylbh.modules.member.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消费者列表
 */
@Service("consumerService")
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, ConsumerVO> implements ConsumerService {
    @Autowired
    private ConsumerMapper consumerMapper;

    /**
     * 消费者数据列表
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(ConsumerSelectDTO params) {
//        return new PageUtils(consumerMapper.selectConsumerList(params),
//                consumerMapper.getConsumerCount(params),
//                params.getLimit(),
//                params.getPage());
        Page<Object> page = new Page<>(params.getPage(), params.getLimit());
        List<ConsumerVO> list = consumerMapper.selectConsumerList(params, page);
        return new PageUtils(new Page<ConsumerVO>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 会员邀请详情
     * @param recommendId
     * @return
     */
    @Override
    public InviteMembersVO getInviteMembersDetailsById(String recommendId) {
        return consumerMapper.selectInviteMembersDetailsById(recommendId);
    }

    @Override
    public void selectAcc(HttpServletResponse response) throws NormalException {
        List<AccExcel> accExcels = consumerMapper.selectAcc();
        for (AccExcel vo : accExcels) {
            List<Integer> count = consumerMapper.selectOrderCount(vo.getId());
            for (int i = 0; i < count.size(); i++) {
                if (vo.getCount() == 0)
                    vo.setCount(count.get(i));
                else
                    vo.setCount(count.get(i) + vo.getCount());
            }
        }
        ExcelUtils.exportExcel(accExcels, "","",AccExcel.class,System.currentTimeMillis() + ".xls",response);
    }

    /**
     * 导出邀请记录
     * @param request
     * @param response
     */
    @Override
    public void exportInvitationRecordExcel(HttpServletRequest request, HttpServletResponse response) throws NormalException {
        String buyState = request.getParameter("buyState");
        String keywords = request.getParameter("keywords");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        List<InvitationRecordExcel> excel = consumerMapper.exportInvitationRecordExcel(buyState, keywords, startTime, endTime);
        ExcelUtils.exportExcel(excel, "邀请记录", "", InvitationRecordExcel.class, System.currentTimeMillis() + ".xls", response);
    }


}
